﻿/*
 * SRSTransformCache
 * 
 * initial implementation : 20.10.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;
using Petra.Gis.ReferenceSystemServices;
using Petra.Gis.ReferenceSystemServices.Transformations;

namespace Petra.Gis.RenderingServices.PlanarBase 
{

  /// <summary>
  /// Cache for transformations used when rendering GIS data on map.
  /// Cashing avoids lookup into main transform storage
  /// cache saves time necessary to search proper transform
  /// </summary>
  public class SRSTransformCache
  {

    protected MapBase _map;
    protected TransformList _cache;


    protected SRSTransformCache()
    {
      _cache = new TransformList();
    }





    public SRSTransformCache(MapBase map)
      :this()
    {
      _map = map;
    }




    //public void Clear()
    //{
    //  _cache = new TransformList();
    //}




    /// <summary>
    /// search for transform from supplied coordinate system
    /// into MapBase.ProjectedCoordinateSystem.
    /// Search starts in local cache,
    /// if no sutable transform found locally, creates new by searching complete transform db
    /// if forward only transform requested then forward only is stored to local cache
    /// if inverse requester both forward and reverse is stored ;-)
    /// </summary>
    /// <param name="dataSourceCS"></param>
    /// <returns>prepared coordinate transform, and this transform is also stored in cache</returns>
    public SRSTransform GetTransform(CoordinateSystem dataSourceCS, bool getInverseTransform)
    {

      //return Petra.Gis.DocumentService.Document.Transforms.GetTransform(dataSourceCS, _map.ProjectedCoordinateSystem, getInverseTransform);
      SRSTransform tmp = Petra.Gis.Environment.SearchTransformationBy(dataSourceCS, _map.ProjectedCoordinateSystem);

      if (getInverseTransform)
        return tmp.Inverse();
      else
        return tmp;


      //// first look into local map cache for exactly same CS..
      //// including inverse check
      //foreach (SRSTransform t in _cache)
      //  if (t.Source.EqualParams(dataSourceCS, CsCompareType.CompareAllParams) &&
      //      t.Target.EqualParams(_map.ProjectedCoordinateSystem, CsCompareType.CompareAllParams) &&
      //      t.IsInverse == getInverseTransform)
      //    return t;

      //// ok, not found ? create new transform
      //// this call will throw error if no transform found
      //SRSTransform tmp = SearchTransform.ByCoordinateSystems(dataSourceCS, _map.ProjectedCoordinateSystem);
      //_cache.Add(tmp);

      ////if inverse is requested get also inverse version...
      //if (getInverseTransform)
      //{
      //  if (tmp.IsInversible)
      //    tmp = tmp.Inverse();
      //  else
      //    tmp = SearchTransform.ByCoordinateSystems(_map.ProjectedCoordinateSystem, dataSourceCS);

      //  _cache.Add(tmp);
      //}

      //return tmp;

    }

  }
}
